package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.enums.NoticeEnums;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequiredPermission(name="公告页面",expression="notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo){

        PageInfo<Notice> pageInfo = noticeService.query(qo);

        model.addAttribute("pageInfo", pageInfo);

//        显示界面的公告程度
        model.addAttribute("level", NoticeEnums.values());
        return "/notice/list";
    }


    @RequiredPermission(name="公告删除",expression="notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {

            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }



    @RequestMapping("/show")
    public String show(Model model,Long id,boolean read){
        Notice notice = noticeService.get(id);
        if(!read){
            noticeService.updateRead(id,UserContext.getCurrentUser().getId());
        }
        model.addAttribute("notice",notice);
        return "/notice/show";
    }

    @RequestMapping("/deliver")
    public String deliver(Long id){
    noticeService.deliver(id);
    return "redirect:/notice/list";
    }

    @RequestMapping("/data")
    @ResponseBody
    public String  data(){
        NoticeQueryObject qo = new NoticeQueryObject();
        qo.setRead(false);
        qo.setPageSize(0);
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        UserContext.setNoticeMessage(pageInfo);
        return null;
    }
}
